package io.jt808.proto.req;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 存储多媒体数据检索应带
 */
@Proto("0802")
@Data
public class JT0802 implements IProto {
    /**
     * 应答流水号
     */

    private short responseMessageSerialNo;

    /**
     * 多媒体数据总项数
     */

    private short multimediaSearchDataItemsCount;

    /**
     * 检索项
     */
    private List<MultimediaSearchDataItem> dataItems;

    public byte[] writeToBytes() {
        AutoBuffer buff = new AutoBuffer();
        buff.writeShort(responseMessageSerialNo);
        buff.writeShort(multimediaSearchDataItemsCount);
        for (MultimediaSearchDataItem item : dataItems) {
            buff.writeInt(item.multimediaId);
            buff.writeByte(item.multimediaType);
            buff.writeByte(item.channelId);
            buff.writeByte(item.eventCode);
            buff.writeBytes(item.position.writeToBytes());
        }
        return buff.array();
    }

    public void readFromBuf(AutoBuffer buffer) {
        responseMessageSerialNo = buffer.readShort();
        multimediaSearchDataItemsCount = buffer.readShort();
        dataItems = new ArrayList<>();
        while (buffer.isReadable()) {
            MultimediaSearchDataItem item = new MultimediaSearchDataItem();
            item.multimediaId = buffer.readInt();
            item.multimediaType = buffer.readByte();
            item.channelId = buffer.readByte();
            item.eventCode = buffer.readByte();
            JT0200 jt0200 = new JT0200();
            jt0200.readFromBuf(buffer.slice(buffer.readerIndex(), 28));
            buffer.skipBytes(28);
            item.position = jt0200;
        }
    }

    public class MultimediaSearchDataItem {
        //媒体ID

        private int multimediaId;
        /**
         * 多媒体类型
         */
        private byte multimediaType;
        /**
         * 通道ID
         */
        private byte channelId;
        /**
         * 事件项编码
         */
        private byte eventCode;
        /**
         * 位置信息汇报
         */
        private JT0200 position;
    }
}